package com.peas.platform.module.icims.budget.apply.domain.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.peas.platform.framework.mybatisplus.domain.BaseDomainImpl;
import com.peas.platform.framework.mybatisplus.repository.BaseRepository;
import com.peas.platform.module.icims.base.sys.service.SequenceService;
import com.peas.platform.module.icims.budget.apply.adapter.repository.BudgetOneRepository;
import com.peas.platform.module.icims.budget.apply.adapter.repository.po.BudgetOnePo;
import com.peas.platform.module.icims.budget.apply.domain.BudgetOneDomain;
import com.peas.platform.module.icims.budget.apply.dto.*;
import com.peas.platform.module.icims.budget.apply.service.*;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

@Component
public class BudgetOneDomainImpl extends BaseDomainImpl<BudgetOneDto, BudgetOnePo> implements BudgetOneDomain {

    @Resource
    private BudgetOneRepository budgetOneRepository;
    @Resource
    private SequenceService sequenceService;
    @Resource
    private BudgetTwoService budgetTwoService;
    @Resource
    private BudgetOneItemService budgetOneItemService;
    @Resource
    private BudgetOneYearService budgetOneYearService;
    @Resource
    private BudgetOneKpiService budgetOneKpiService;
    @Resource
    private BudgetOneFileService budgetOneFileService;

    @Override
    public BaseRepository<BudgetOnePo> getMapper() {
        return budgetOneRepository;
    }

    @Override
    public Class<? extends BaseRepository<BudgetOnePo>> getRepositoryClass() {
        return BudgetOneRepository.class;
    }

    @Transactional
    @Override
    public int save(BudgetOneDto dto) {
        if (StrUtil.isBlank(dto.getCode())) {
            String code = sequenceService.genSequence("base", "BudgetOneDto", "BO", 4, 1);
            dto.setCode(code);
        }
        int result = super.save(dto);
        this.checkRepetition("code", dto.getCode());
        return result;
    }

    @Transactional
    @Override
    public int updateById(BudgetOneDto dto) {
        int result = super.updateById(dto);
        this.checkRepetition("code", dto.getCode());
        return result;
    }

    @Transactional
    @Override
    public int removeById(Serializable id) {
        List<BudgetTwoDto> list = budgetTwoService.listByField("budgetOneId", id);
        Assert.isTrue(CollUtil.isEmpty(list), "有关联的二上预算编制存在");
        // items
        List<BudgetOneItemDto> ptids = budgetOneItemService.listByField("budgetOneId", id);
        budgetOneItemService.removeBatchByList(ptids);
        // years
        List<BudgetOneYearDto> ptyds = budgetOneYearService.listByField("budgetOneId", id);
        budgetOneYearService.removeBatchByList(ptyds);
        // kpis
        List<BudgetOneKpiDto> ptkds = budgetOneKpiService.listByField("budgetOneId", id);
        budgetOneKpiService.removeBatchByList(ptkds);
        // files
        List<BudgetOneFileDto> ptfds = budgetOneFileService.listByField("budgetOneId", id);
        budgetOneFileService.removeBatchByList(ptfds);
        return super.removeById(id);
    }

}
